package com.lineage.server.timecontroller.quest;

import com.lineage.data.quest.WizardLv30_1;
import com.lineage.server.model.Instance.L1DoorInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.templates.L1QuestUser;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldQuest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class W30_Timer extends TimerTask {
  private static final Log _log = LogFactory.getLog(W30_Timer.class);
  
  private ScheduledFuture<?> _timer;
  
  private int _qid = -1;
  
  public void start() {
    this._qid = WizardLv30_1.QUEST.get_id();
    int timeMillis = 1000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 1000L, 1000L);
  }
  
  public void run() {
    try {
      HashMap<Integer, L1Object> mapList = new HashMap<>();
      mapList.putAll(World.get().getVisibleObjects(201));
      ArrayList<L1QuestUser> questList = WorldQuest.get().getQuests(this._qid);
      Object[] array;
      int length = (array = questList.toArray()).length;
      int j = 0;
      while (j < length) {
        Object object = array[j];
        L1QuestUser quest = (L1QuestUser)object;
        int i = 0;
        Iterator<L1Object> iterator = mapList.values().iterator();
        while (iterator.hasNext()) {
          L1Object obj = iterator.next();
          if (obj instanceof com.lineage.server.model.Instance.L1MerchantInstance)
            continue; 
          if (obj.get_showId() != quest.get_id())
            continue; 
          if (obj.getX() == 32867 && obj.getY() == 32912)
            i++; 
          if (obj.getX() == 32867 && obj.getY() == 32927)
            i++; 
          if (obj.getX() == 32860 && obj.getY() == 32920)
            i++; 
          if (obj.getX() != 32875 || obj.getY() != 32920)
            continue; 
          i++;
        } 
        if (i >= 4) {
          Iterator<L1NpcInstance> iterator2 = quest.npcList().iterator();
          while (iterator2.hasNext()) {
            L1NpcInstance npc = iterator2.next();
            if (npc instanceof L1DoorInstance) {
              L1DoorInstance door = (L1DoorInstance)npc;
              if (door.getDoorId() != 10003)
                continue; 
              door.open();
              Thread.sleep(50L);
            } 
          } 
          Iterator<L1PcInstance> iterator3 = quest.pcList().iterator();
          while (iterator3.hasNext()) {
            L1PcInstance pc = iterator3.next();
            if (pc.getX() == 32868 && pc.getY() == 32919) {
              L1Teleport.teleport(pc, 32929, 32798, (short)201, 5, true);
              Thread.sleep(50L);
            } 
          } 
        } else {
          Iterator<L1NpcInstance> iterator4 = quest.npcList().iterator();
          while (iterator4.hasNext()) {
            L1NpcInstance npc = iterator4.next();
            if (npc instanceof L1DoorInstance) {
              L1DoorInstance door = (L1DoorInstance)npc;
              if (door.getDoorId() != 10003)
                continue; 
              door.close();
              Thread.sleep(50L);
            } 
          } 
        } 
        j++;
      } 
      mapList.clear();
      questList.clear();
    } catch (Exception e) {
      _log.error("不死族的叛徒 (法师30级以上官方任务)时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      W30_Timer w30Timer = new W30_Timer();
      w30Timer.start();
    } 
  }
}
